#include "junix.h"
#include <tr1/unordered_map>

using namespace std;

typedef std::pair<std::string,int> PSI;

bool operator< (const PSI& lhs,const PSI& rhs) {
	return lhs.second<rhs.second;
}

int main(int argc, char **argv)
{
	typedef std::tr1::unordered_map<std::string,int> DICT;
	DICT dict;

	std::ifstream file("BIGTXT");
	if (!file) {
		std::cerr<<"File open error\n";
		exit(EXIT_FAILURE);
	}

	std::string s;
	while (file>>s) {
		++ dict[s];
	}

	file.close();

	/*
	boost::function<bool(PSI,PSI)> Compare;
	using boost::bind;
	Compare = bind( std::less<int>(),
			        bind(&PSI::second, _1),
					bind(&PSI::second, _2));
	
	PSI t1(string("hello"),1);
	PSI t2(string("world"),2);
	cout<<boolalpha<<Compare(t1,t2);
	*/

	jtime T;
	T.start();

	std::priority_queue<PSI> pq;
	DICT::iterator pos = dict.begin(), end = dict.end();

	for (int i=0;i<10;++i,++pos) {
		pq.push(make_pair(pos->first,pos->second));
	}

	for (;pos!=end;++pos) {
		if (pos->second > pq.top().second) {
			pq.pop();
			pq.push(make_pair(pos->first,pos->second));
		}
	}

	while (!pq.empty()) {
		cout<<"("<<pq.top().first
			<<","<<pq.top().second<<")\n";

		pq.pop();
	}

	T.end();
	T.print();
}
